{% extends "base/base.html" %}
{% load static %}
{% block css %}
    <!--LOADING 时间插件 FOR PAGE-->
        <link type="text/css" rel="stylesheet" href="{% static 'vendors/bootstrap-colorpicker/css/colorpicker.css' %}">
        <link type="text/css" rel="stylesheet" href="{% static 'vendors/bootstrap-datepicker/css/datepicker.css' %}">
        <link type="text/css" rel="stylesheet"
              href="{% static 'vendors/bootstrap-daterangepicker/daterangepicker-bs3.css' %}">
        <link type="text/css" rel="stylesheet"
              href="{% static 'vendors/bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css' %}">
        <link type="text/css" rel="stylesheet"
              href="{% static 'vendors/bootstrap-timepicker/css/bootstrap-timepicker.min.css' %}">
        <link type="text/css" rel="stylesheet" href="{% static 'vendors/bootstrap-clockface/css/clockface.css' %}">
        <link type="text/css" rel="stylesheet" href="{% static 'vendors/bootstrap-switch/css/bootstrap-switch.css' %}">

    <!--LOADING STYLESHEET FOR UPLOAD PAGE-->
    <link type="text/css" rel="stylesheet" href="{% static 'vendors/jquery-file-upload/css/jquery.fileupload.css' %}">
    <link type="text/css" rel="stylesheet"
          href="{% static 'vendors/jquery-file-upload/css/jquery.fileupload-ui.css' %}">
    <link type="text/css" rel="stylesheet" href="{% static 'vendors/jquery-file-upload/css/blueimp-gallery.min.css' %}">

{% endblock %}

{% block right_menus %}
    <li><i class="fa fa-home"></i>&nbsp;<a href="{% url 'my_admin:list_channel' %}">首页</a>&nbsp;&nbsp;<i
            class="fa fa-angle-right"></i>
    </li>
    <li><a href="{% url 'my_admin:list_channel' %}">渠道列表</a>&nbsp;<i class="fa fa-angle-right"></i></li>
    <li class="active">修改产品价格</li>
{% endblock %}

{% block context %}

    <div class="row">
        <div class="col-lg-12">
            <div class="portlet box portlet-blue">
                <div class="portlet-header">
                    <div class="caption">修改"{{ channel_product_obj.product.name }}"价格</div>
                    <div class="tools"><i class="fa fa-chevron-up"></i><i data-toggle="modal"
                                                                          data-target="#modal-config"
                                                                          class="fa fa-cog"></i><i
                            class="fa fa-refresh"></i><i class="fa fa-times"></i>
                    </div>
                </div>
                <div class="portlet-body pan">
                    <form action="" class="form-horizontal form-separated" id="my_form" method="post">

                        {% csrf_token %}
                        <div class="form-body pdl">

                            <div class="form-group">
                                <label class="col-md-3 control-label">价格</label>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" name="sum_price"
                                           placeholder="如果是百分比，请换算成小数">
                                </div>
                            </div>
                            {% if channel_obj %}
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品</label>
                                <div class="col-md-6">
                                    <select class="form-control" name="product">
                                        {% for i in not_set %}
                                            <option value="{{ i.id }}" selected>{{ i.name }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                        {% endif %}


                            <div class="form-group">
                                <label class="col-md-3 control-label">生效时间</label>
                                <div class="col-md-6">
                                    <div class="input-group datetimepicker-default date">
                                        <input readonly type="text" name="update_time" class="form-control"/><span
                                            class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="form-actions">
                            <div class="col-md-offset-3 col-md-9">
                                {% if product_info %}
                                    <button type="submit" class="btn btn-primary">修改</button>
                                    &nbsp;{% else %}
                                    <button type="submit" class="btn btn-primary">提交</button>
                                {% endif %}
                                {#                                <button id="file_upload" type="submit" class="btn btn-primary" style="display: none">#}
                                {#                                    提交#}
                                {#                                </button>#}
                                <button onclick="return_button()" type="button" class="btn btn-default">返回</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
{% endblock %}

{% block js %}
    {#    <script src="{% static 'vendors/bootstrap-datepicker/js/bootstrap-datepicker.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-daterangepicker/daterangepicker.js' %}"></script>#}
    {#    <script src="{% static 'vendors/moment/moment.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-timepicker/js/bootstrap-timepicker.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-clockface/js/clockface.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-colorpicker/js/bootstrap-colorpicker.js' %}"></script>#}
    {#    <script src="{% static 'vendors/bootstrap-switch/js/bootstrap-switch.min.js' %}"></script>#}
    {#    <script src="{% static 'vendors/jquery-maskedinput/jquery-maskedinput.js' %}"></script>#}
    {#    <script src="{% static 'vendors/charCount.js' %}"></script>#}
    {#    <script src="{% static 'js/form-components.js' %}"></script>#}
    <script src="{% static 'vendors/moment/moment.js' %}"></script>
    <script src="http://cdn.bootcss.com/bootstrap-datetimepicker/2.1.30/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
    <script src="{% static 'vendors/bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js' %}"></script>
    <script src="{% static 'vendors/bootstrap-timepicker/js/bootstrap-timepicker.js' %}"></script>
    <!--for upload page-->
    <script src="{% static 'vendors/jquery-file-upload/js/vendor/jquery.ui.widget.js' %}"></script>
    <script src="{% static 'vendors/jquery-file-upload/js/jquery.iframe-transport.js' %}"></script>
    <script src="{% static 'vendors/jquery-file-upload/js/jquery.fileupload.js' %}"></script>

    <!--for 验证 page-->
    <script src="{% static 'vendors/jquery-validate/jquery.validate.min.js' %}"></script>
    <script src="{% static 'js/form-validation.js' %}"></script>
    <script src="{% static 'my/update_channel_product_price.js' %}"></script>

    <script type="text/javascript">
        $(".datetimepicker-default").datetimepicker({  //时间格式化
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            format: 'YYYY-MM-DD HH:mm',
        });

    </script>

    <script>

        var flag = false;
        $.validator.setDefaults({
            submitHandler: function (form) {
                if ("{{ channel_product_obj }}") {
                    if (flag) {
                        return false
                    }
                    flag = true;
                    edit_data()
                }
                else {
                    if (flag) {
                        return false
                    }
                    flag = true;
                    commit_data()

                }
            }
        });
        // 添加
        var post_url = "{% url 'my_admin:add_channel_product' channel_id %}";
        function commit_data() {
            var data = $("form").serialize();
            $.ajax({
                type: 'POST',
                url: post_url,
                data: data,
                success: function (data) {
                    if (data.status == "ok") {
                        alert(data.msg);
                        // history.go(-1)
                        window.location.href = "{% url 'my_admin:channel_product_list' channel_id %}"
                    }
                    else {
                        alert(data.msg);
                        window.location.reload()
                    }
                },
                error: function (request) {
                    alert("服务器异常, 请刷新重试");

                }
            });
        }
        // 修改
        function edit_data() {
            post_url = "/admin/edit_channel_product/{{ channel_product_obj.id }}";
            var data = $("form").serialize();
            $.ajax({
                type: 'POST',
                url: post_url,
                data: data,
                success: function (data) {
                    if (data.status == "ok") {
                        alert(data.msg);
                        window.location.href = "{% url 'my_admin:channel_product_list' channel_id %}"
                        // history.go(-1)
                    }
                    else {
                        alert(data.msg);
                        window.location.reload()
                    }
                },
                error: function (request) {
                    alert("服务器异常");
                }
            });
        }
        // 数据回显
        $(document).ready(function () {
            if ("{{ channel_product_obj }}") {
                $("input[name='update_time']").val("{{ channel_product_obj.update_time |date:"Y-m-d" }}");
                $("input[name='sum_price']").val("{{ channel_product_obj.sum_price }}");
            }
        });
    </script>
{% endblock %}